package com.wawei.seeds.services;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;

import com.wawei.seeds.model.Appointment;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.remote.AppointmentRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutMainServiceRestfulService;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutNotifyHandler;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 *
 * Created by t27891 on 15/6/22.
 */
public class WalnutAppointmentServiceHandler implements ServiceHandler {
    public static final String APPOINTMENT_ME_BOOL = "appointment_me_or_not";
    private Context context;

    public WalnutAppointmentServiceHandler(Context context) {
        this.context = context;
    }

    @Override
    public void retrieveData(final RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag) {
        List<WalnutMainEvent> items = new ArrayList<>(0);
        WalnutMainEvent item;

        if(null == bundle || !bundle.getBoolean(APPOINTMENT_ME_BOOL)){
            doAppointmentData(restAdapter, bundle, handler, handlerTag);
        }
        else if(bundle.getBoolean(APPOINTMENT_ME_BOOL)){
            doAppointmentMeData(restAdapter, bundle, handler, handlerTag);
        }
    }

    /**
     * list all appointments I want
     * @param restAdapter restful service adapter for doing remote service call
     * @param bundle    the calling condition
     * @param handler   notification handler for async call
     * @param handlerTag    the tag for specific call action
     */
    private void doAppointmentData(final RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag){
        switch (handlerTag.intValue()) {
            case WalnutNotifyHandler.LOAD_NEW_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.LOAD_OLD_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.INIT_ACHIEVE_DATA:
                WalnutMainServiceRestfulService restfulService = restAdapter.create(WalnutMainServiceRestfulService.class);
                restfulService.getMyAppointment(String.valueOf(PrefUtils.getUserId(context)), new Callback<AppointmentRestfulWrapper>() {
                    @Override
                    public void success(AppointmentRestfulWrapper appointmentRestfulWrapper, Response response) {
                        List<Appointment> appointments = appointmentRestfulWrapper.getBookingBeans();
                        List<WalnutMainEvent> events = null;
                        for(int i=0; null != appointments && i < appointments.size(); i++){
                            Appointment appointment = appointments.get(i);
                            if(null != appointment.items) {
                                for(WalnutMainEvent mainEvent : appointment.items){
                                    if(null != appointment.provider) {
                                        mainEvent.publisherHeadPortrait = appointment.provider.headPortrait;
                                    }
                                    mainEvent.bookingState = appointment.bookingState;
                                }
                                if (null == events) {
                                    events = appointment.items;
                                } else {
                                    events.addAll(appointment.items);
                                }
                            }
                        }

                        if(null == events) {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                        }
                        else {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, events.toArray());
                        }
                    }

                    @Override
                    public void failure(RetrofitError retrofitError) {
                        RetrofitNetworkService.handleError(handler,handlerTag, retrofitError);
                    }
                });
                break;
            default:
                break;
        }
    }

    /**
     * list all appointments I am taken
     * @param restAdapter restful service adapter for doing remote service call
     * @param bundle    the calling condition
     * @param handler   notification handler for async call
     * @param handlerTag    the tag for specific call action
     */
    private void doAppointmentMeData(final RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag){
        switch (handlerTag.intValue()) {
            case WalnutNotifyHandler.LOAD_NEW_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.LOAD_OLD_ACHIEVE_DATA:
                RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                break;
            case WalnutNotifyHandler.INIT_ACHIEVE_DATA:
                WalnutMainServiceRestfulService restfulService = restAdapter.create(WalnutMainServiceRestfulService.class);
                restfulService.getAppointmentMe(String.valueOf(PrefUtils.getUserId(context)), new Callback<AppointmentRestfulWrapper>() {
                    @Override
                    public void success(AppointmentRestfulWrapper appointmentRestfulWrapper, Response response) {
                        List<Appointment> appointments = appointmentRestfulWrapper.getBookingBeans();
                        List<WalnutMainEvent> events = null;
                        for(int i=0; null != appointments && i < appointments.size(); i++){
                            Appointment appointment = appointments.get(i);
                            if(null != appointment.items) {
                                for(WalnutMainEvent mainEvent : appointment.items){
                                    if(null != appointment.user) {
                                        mainEvent.publisherHeadPortrait = appointment.user.headPortrait;
                                        mainEvent.booked = appointment.user.userName;
                                        mainEvent.phone = appointment.user.mobile;
                                    }
                                    mainEvent.bookingId = appointment.bookingId;
                                }
                                if (null == events) {
                                    events = appointment.items;
                                }
                                else {
                                    events.addAll(appointment.items);
                                }
                            }
                        }

                        if(null == events) {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                        }
                        else {
                            RetrofitNetworkService.handleSuccess(handler, handlerTag, events.toArray());
                        }
                    }

                    @Override
                    public void failure(RetrofitError retrofitError) {
                        RetrofitNetworkService.handleError(handler,handlerTag, retrofitError);
                    }
                });
                break;
            default:
                break;
        }
    }
}
